#define _CRT_SECURE_NO_WARNINGS 1


#include<string>
#include<iostream>

using namespace std;

void Reverse(char* left, char* right)
{
	while (left < right)
	{
		swap(*left++, *right--);
	}
}

string reverseStr(string s, int k)
{
	int size = s.size();
	int left = 0, right = -1;
	while (right < size)
	{
		right += 2 * k;
		Reverse(&s[left], &s[left + k - 1]);
		if (size - right - 1 < k)
		{
			Reverse(&s[left], &s[size - 1]);
			break;
		}
		else if (size - right - 1 < 2 * k && size - left - 1 >= k)
		{
			Reverse(&s[left + 1], &s[left + k]);
			break;
		}
		left = right;
	}
	return s;
}

int main()
{
	string s = "abcd";
	cout << reverseStr(s, 2) << endl;

	return 0;
}